package com.clip.action;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.action.util.SessionAttributeKey;
import com.clip.entity.AlertUser;
import com.clip.entity.ClipEvent;
import com.clip.entity.ClipeventPic;
import com.clip.entity.Clipmessage;
import com.clip.entity.RequestEvent;
import com.clip.entity.ShareEvent;
import com.clip.entity.Sharemapping;
import com.clip.entity.User;
import com.clip.service.ClipeventPicService;
import com.clip.service.ContactService;
import com.clip.service.EventService;
import com.clip.service.UserService;
import com.clip.util.Struts2Utils;
import com.clip.util.ToolsUtils;
import com.clip.util.URLReplace;
import com.clip.util.json.JSONArray;
import com.clip.util.json.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class EventAction extends ActionSupport {
	@Autowired
	private EventService eventService;
	@Autowired
	private ContactService contactService;
	@Autowired
	private UserService userService;
	@Autowired
	private ClipeventPicService picService;
	
	public void finishEvent(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Calendar calendar=Calendar.getInstance();
		String message=eventService.finishEvent(currentUser,eventid,calendar);
		
		if(message==null){
			Struts2Utils.renderHtml("{result:1}");
		}else{
			Struts2Utils.renderHtml("{result:0,msg:'"+message+"'}");
		}
	}
	
	public void cancelClock(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		String message=eventService.cancelClock(currentUser,eventid);
		JSONObject reObj=new JSONObject();
		if(message==null){
			reObj.put("result", 1);
		}else{
			reObj.put("result", 0);
			reObj.put("errormsg", message);
		}
		Struts2Utils.renderJson(reObj.toJSONString());
	}
	
	public void savetemp(){
		HttpServletRequest request=Struts2Utils.getRequest();
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		if(currentUser!=null){
			ClipEvent ce=new ClipEvent();
			ce.setId(0);
			ce.setTitle(title);
			ce.setBegintime(begintime);
			ce.setAddress("");
			ce.setContent("");
			ce.setEventtype(1);
			ce.setEndtime(0l);
			ce.setIswholeday(1);
			ce.setUid(currentUser.getId());
			session.setAttribute(SessionAttributeKey.TEMPCLIP, ce);
			Struts2Utils.renderHtml("{result:1}");
		}else
			Struts2Utils.renderHtml("{result:0}");
	}
	
	/*public void getDetailEvent(){
		String str=eventService.getDetailEvent(eventid);
		Struts2Utils.renderHtml(str);
	}*/
	public void submitClipData(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		ClipEvent result=null;
		if(eventtype==null||eventtype.length()==0)
			eventtype="other";
		if(this.eventid!=null&&this.eventid!=0){
			result=eventService.updateClipEvent(eventid,cliptype,title,address,content,begintime,endtime,alerttime,deadlinetime,currentUser,now,status,mobilenotice,mobilelist,iswholeday,share2userlist);
		}else
			result=eventService.createClipEvent(cliptype,title,address,content,begintime,endtime,alerttime,deadlinetime,currentUser,now,status,mobilenotice,mobilelist,iswholeday,share2userlist);
		if(status==1&&result.getId()>0&&picid>0){
			this.picService.updateClipeventpic(result.getId(),picid);
		}
		if(result.getId()>0)
			if(result.getCliptype()==3||result.getCliptype()==4){
				Struts2Utils.renderHtml("{result:1,eventid:'"+result.getId()+"',cliptype:3,shareid:'"+result.getShareid()+"'}");
			}else
				Struts2Utils.renderHtml("{result:1,eventid:'"+result.getId()+"',cliptype:"+result.getCliptype()+"}");
		else{
			Struts2Utils.renderHtml("{result:0}");
		}
	}
	public void addEventClock(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		this.eventService.addClock(currentUser,eventid,alerttime,mobilenotice);
		Struts2Utils.renderHtml("{result:1}");
	}
	public void deleteEvent(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		this.eventService.deleteEvent(currentUser, eventid);
		Struts2Utils.renderHtml("{result:1}");
	}
	
//	public void acceptInvitedClip(){
//		HttpServletRequest request=Struts2Utils.getRequest();
//		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
//		ClipEvent clipEvent=this.eventService.acceptInvitedClip(currentUser,eventid);
//	}
	public void ignoreInvitedClip(){
			
	}
	public void refuseInvitedClip(){
		
	}
	
	public void rebackEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		ClipEvent clipEvent=this.eventService.rebackEvent(currentUser, eventid,now);
		Struts2Utils.renderHtml("{result:1,repliedclip:"+clipEvent.getReliedclip()+",eventid:"+clipEvent.getId()+"}");
	}
	
	public void modifyEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		ClipEvent clipEvent=this.eventService.modifyEvent(currentUser, eventid);
		Struts2Utils.renderHtml("{result:1,eventid:"+clipEvent.getId()+"}");
	}
	
	public void deletePatchCacheEvents(){
		HttpServletRequest request=Struts2Utils.getRequest();
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		this.eventService.deletePatchCacheEvents(currentUser, eventids);
		Struts2Utils.renderHtml("{result:1}");
	}
	@Deprecated
	public void getDateEvents(){
		HttpServletRequest request=Struts2Utils.getRequest();
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		List<ClipEvent> list=this.eventService.getEventsByYear(currentUser,year,2);
		Map map=organizeDateEventsAsXml(list);
		Struts2Utils.renderXml(ToolsUtils.getDOM(map));
	}
	
	public void setEventImport(){
		this.eventService.setEventImport(isImport,eventid);
	}
	public void addreply(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		String msg=this.eventService.replyMessage(eventid, content, currentUser,now);
		JSONObject reObj=new JSONObject();
		if(msg==null||msg.length()==0){
			reObj.put("result", 1);
			reObj.put("replytime",now.getTimeInMillis());
		}else{
			reObj.put("result", 0);
			reObj.put("errormsg", msg);
		}
		Struts2Utils.renderJson(reObj.toJSONString());
	}
	
	public void loadClipMessage(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		List<Clipmessage> list=this.eventService.loadAllMessageByEventid(this.eventid);
		Map temp1=new LinkedHashMap();
		for (Clipmessage clipmessage : list) {
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("content", ToolsUtils.xmlCDATA(clipmessage.getContent()));
			temp2.put("replytime", clipmessage.getReplytime());
			int uid=clipmessage.getUid();
			List name0_email1=this.contactService.getContactNameAndEmail(currentUser,uid);
			temp2.put("uid", uid);
			temp2.put("name", name0_email1.get(0));
			//temp2.put("email", name0_email1.get(1));
			String smalllogo=(String) this.userService.getUserLogoByUid(uid).get(1);
			temp2.put("useriscc", this.userService.checkUserIsCC(uid));
			temp2.put("logo", ToolsUtils.xmlCDATA(smalllogo));
			temp2.put("msgtype", clipmessage.getMsgtype());
			temp2.put("msgtypestr",Clipmessage.getMsgtypeMeaning(clipmessage.getMsgtype()));
			temp1.put("Clipmessage mid="+clipmessage.getId(), temp2);
		}
		Struts2Utils.renderXml(ToolsUtils.getDOM(temp1));
	}
	
	public void updateMemoContent(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		String reply=this.eventService.updateMemoContent(eventid,title,alerttime,mobilenotice,currentUser);
		if(reply.length()==0){
			Struts2Utils.renderHtml("{result:1}");
		}else
			Struts2Utils.renderHtml("{result:0,msg:'"+reply+"'}");
	}
	
	private Map<String,Map> organizeDateEventsAsXml(List<ClipEvent> list){
		Map<String,Map> temp1=new HashMap<String,Map>();
		for (ClipEvent clipEvent : list) {
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", clipEvent.getId());
			temp2.put("eventtype", ClipEvent.eventtypeInt2Str(clipEvent.getEventtype()));
			temp2.put("starttime", clipEvent.getBegintime().toString());
			temp2.put("content", clipEvent.getContent()==null?"":clipEvent.getContent());
			temp2.put("clocktime", clipEvent.getAlerttime().toString());
			temp2.put("address", clipEvent.getAddress()==null?"":clipEvent.getAddress());
			temp2.put("title", clipEvent.getTitle());
			Calendar calendar=Calendar.getInstance();
			calendar.setTimeInMillis(clipEvent.getBegintime());
			
			temp1.put("DateEvent day="+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DAY_OF_MONTH), temp2);
		}
		return temp1;
	}
	private int CALENDARPAGESIZE=10;
	public void getAllEventsByDay(){//我的档期+好友分享
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long lastquerycreattime=0l;
		int lastcurrentpage=-1;
		Calendar now=Calendar.getInstance();
		if(needmore==1){
			lastquerycreattime=(Long) session.getAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYCREATETIME);
			lastcurrentpage=(Integer) session.getAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYPAGENUM);
		}
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Map map=new LinkedHashMap();
		List list=null;
		if(queryby.equals("own")){
			list=this.eventService.queryClipList(queryby,currentUser,lastcurrentpage+1,CALENDARPAGESIZE,lastquerycreattime,content,firstDate,null,false);
			if(list!=null&&list.size()>0){
				for (Object object : list) {
					ClipEvent clipevent=(ClipEvent) object;
					Map submap=orgAllClipEvents(clipevent,currentUser,now);
					if(submap==null)
						continue;
					submap.put("isshielded", false);
					int isself=clipevent.getUid()==currentUser.getId()?1:0;
					map.put("ClipEvent isuserself="+isself+" id="+clipevent.getId(),submap);
				}
				ClipEvent clipevent=(ClipEvent) list.get(0);
				if(lastquerycreattime==0l)
					session.setAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYCREATETIME,clipevent.getCreatetime());
				session.setAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYPAGENUM,lastcurrentpage+1);
			}
		}else if(queryby.equals("private")){
			list=this.eventService.queryClipList(queryby,currentUser,lastcurrentpage+1,CALENDARPAGESIZE,lastquerycreattime,content,firstDate,null,false);
			if(list!=null&&list.size()>0){
				for (Object object : list) {
					ClipEvent clipEvent=(ClipEvent) object;
					Map submap=orgAllClipEvents(clipEvent,currentUser,now);
					submap.put("isshielded", false);
					map.put("ClipEvent isuserself=1 id="+clipEvent.getId(),submap);
				}
				ClipEvent clipEvent=(ClipEvent) list.get(0);
				if(lastquerycreattime==0l)
					session.setAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYCREATETIME,clipEvent.getCreatetime());
				session.setAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYPAGENUM,lastcurrentpage+1);
			}
		}else if(queryby.equals("share")){
			list=this.eventService.queryClipList(queryby,currentUser,lastcurrentpage+1,CALENDARPAGESIZE,lastquerycreattime,content,firstDate,null,false);
			if(list!=null&&list.size()>0){
				for (Object object : list) {
					RequestEvent shareEvent=(RequestEvent) object;
					Map submap=orgRequestEvents(shareEvent, currentUser, now);
					submap.put("isshielded", false);
					map.put("ClipEvent isuserself=false id="+shareEvent.getEventid(),submap);
				}
				RequestEvent shareEvent=(RequestEvent) list.get(0);
				if(lastquerycreattime==0l)
					session.setAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYCREATETIME,shareEvent.getCreatetime());
				session.setAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYPAGENUM,lastcurrentpage+1);
			}
			
		}else{
			list=this.eventService.queryClipList(queryby,currentUser,lastcurrentpage+1,0,lastquerycreattime,content,firstDate,null,false);
			if(queryby.equals("invite")){
				for (Object object : list) {
					ShareEvent clipevent=(ShareEvent) object;
					Map submap=orgShareEvents(clipevent,currentUser,now);
					boolean isshielded=this.eventService.checkIsShielded(clipevent.getEventid(), currentUser.getId());
					submap.put("isshielded", isshielded);
//					int accepttype=this.eventService.getInviteEventInterestORNOT(clipevent.getId(),currentUser.getId());
//					submap.put("invitestatus", accepttype);
					map.put("InviteEvent isuserself=false id="+clipevent.getEventid(), submap);
				}
			}else if(queryby.equals("memo")){
				List urllist=this.userService.getUserLogoByUid(currentUser.getId());
				for (Object object : list) {
					ClipEvent clipevent=(ClipEvent) object;
					Map<String,Object> temp2=new HashMap<String,Object>();
					temp2.put("uname", "我");
					temp2.put("unameid", currentUser.getId());
					temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
					temp2.put("starttime", clipevent.getBegintime().toString());
					temp2.put("clocktime", clipevent.getAlerttime().toString());
					temp2.put("title", ToolsUtils.xmlCDATA(clipevent.getTitle()));
					temp2.put("cliptype", clipevent.getCliptype());
					//temp2.put("date", firstDateOfWeek.getTimeInMillis());
					map.put("MemoEvent id="+clipevent.getId(), temp2);
				}
			}
		}
		if(needmore!=1){
			int alertcount=this.eventService.getEventsByAlertDayOnlyCount(firstDate, currentUser);
			int memocount=this.eventService.countMemoEventsByDay(firstDate, currentUser);
			long invitecount=this.eventService.countInvitedEvents(currentUser, firstDate);
			map.put("invitecount", invitecount);
			map.put("alertcount", alertcount);
		}
		Struts2Utils.renderXml(ToolsUtils.getDOM(map));
	}
	
	public void setCalendarDetailQuery(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long lastquerycreattime=0l;
		Calendar now=Calendar.getInstance();
		Object temp=session.getAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYCREATETIME);
		if(temp!=null)
			lastquerycreattime=(Long)temp;
		temp=session.getAttribute(this.queryby+"_"+SessionAttributeKey.LASTQUERYPAGENUM);
		String querycontent=null;
		if(year==0&&month==0&&date==0){
			querycontent=this.eventService.getQueryContentAtCalendar(queryby,currentUser,lastquerycreattime,null,content);
		}else{
			Calendar firstDate=Calendar.getInstance();
			firstDate.clear(Calendar.MILLISECOND);
			firstDate.clear(Calendar.SECOND);
			firstDate.clear(Calendar.MINUTE);
			firstDate.set(Calendar.HOUR_OF_DAY, 0);
			firstDate.set(Calendar.YEAR, year);
			firstDate.set(Calendar.MONTH, month);
			firstDate.set(Calendar.DAY_OF_MONTH,date);
			querycontent=this.eventService.getQueryContentAtCalendar(queryby,currentUser,lastquerycreattime,firstDate,content);
		}
		session.setAttribute(SessionAttributeKey.DETAILCLIPQUERYCONTENT, querycontent);
		Struts2Utils.renderHtml("{result:1}");
	}
	
	
	
	public void getDetailClipidByPosition(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Object object=session.getAttribute(SessionAttributeKey.DETAILCLIPQUERYCONTENT);
		if(object==null){
			Struts2Utils.renderHtml("{result:0,msg:'无效的点击'}");
		}else{
			String querycontent=(String) object;
			int tempeventid=this.eventService.getClipEventIdByPosition(querycontent,eventid,move);
			if(tempeventid==0){
				if(move>0)
					Struts2Utils.renderHtml("{result:0,msg:'已是最后一条档期'}");
				else
					Struts2Utils.renderHtml("{result:0,msg:'已是第一条档期'}");
			}else{
				ClipEvent clipevent=this.eventService.getClipEventById(tempeventid);
				if(clipevent.getUid()==currentUser.getId())
					Struts2Utils.renderHtml("{result:1,eventid:"+clipevent.getId()+"}");
				else
					Struts2Utils.renderHtml("{result:1,shareid:'"+clipevent.getShareid()+"'}");
			}
		}
	}
	
	public Map orgShareEvents(ShareEvent shareEvent,User currentUser,Calendar now){
		Map<String,Object> temp2=new LinkedHashMap<String,Object>();
		boolean isself=shareEvent.getUid()==currentUser.getId();
		int reliedclip=shareEvent.getReliedclip();
		int uid=shareEvent.getUid();
		if(reliedclip>0){
			//不可能
			return null;
		}else{
			if(uid==currentUser.getId()){
				//不可能
				return null;
			}else{
				temp2.put("isself", false);
				String contactname=this.contactService.getUserName(currentUser.getId(),uid);
				temp2.put("unameid",uid);
				temp2.put("uname", contactname);
				List urllist=this.userService.getUserLogoByUid(uid);
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(0)));
				temp2.put("unameslogo", ToolsUtils.xmlCDATA(urllist.get(1)));
			}
//			temp2.put("clipusertype",shareEvent.getEventtype() );
		}
		temp2.put("cliptype", 3);
		temp2.put("seid", shareEvent.getSeid());
		if(shareEvent.getSeid()>0){
			ClipEvent sourceEvent=this.eventService.getDetailClipEvent(shareEvent.getSeid());
			User sourceUser=this.userService.getUserById(sourceEvent.getUid());
			temp2.put("sourceeventid", sourceEvent.getId());
			temp2.put("sourceuid", sourceEvent.getUid());
			temp2.put("sourceuname", this.contactService.getUserName(currentUser.getId(), sourceUser.getId()));
			List sourceuserlist=this.userService.getUserLogoByUid(sourceUser.getId());
			temp2.put("sourceulogo", sourceuserlist.get(1));
		}
		temp2.put("shareid", shareEvent.getShareid());
		temp2.put("starttime", shareEvent.getBegintime().toString());
		temp2.put("endtime", shareEvent.getEndtime().toString());
		temp2.put("invitetime", shareEvent.getInvitetime().toString());
		temp2.put("iswholeday", shareEvent.getIswholeday().toString());
		String content=shareEvent.getContent();
		content=content==null||content.length()==0?"":content;
		temp2.put("content", ToolsUtils.xmlCDATA(URLReplace.replace(content)));
//		temp2.put("address", ToolsUtils.xmlCDATA(shareEvent.getAddress()));
		temp2.put("clipstatus", 1);
		temp2.put("title", ToolsUtils.xmlCDATA(shareEvent.getTitle()));
		temp2.put("eventtype", shareEvent.getEventtype());
		ClipeventPic clipeventPic=this.eventService.getClipeventPic(shareEvent.getEventid());
		if(clipeventPic!=null){
			temp2.put("clipeventpic", clipeventPic.getPicurl());
		}else{
			temp2.put("clipeventpic", "");
		}
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		int timesituation=0;
		if(shareEvent.getBegintime()>=firstDate.getTimeInMillis()&&shareEvent.getBegintime()<secondDate.getTimeInMillis()){
			timesituation= -1;
		}else if(shareEvent.getEndtime()>=firstDate.getTimeInMillis()&&shareEvent.getEndtime()<secondDate.getTimeInMillis()&&shareEvent.getIswholeday()==0){
			timesituation= 1;
		}else{
			timesituation= 0;
		}
		
		temp2.put("timesituation", timesituation);
		temp2.put("reliedclip", shareEvent.getReliedclip().toString());
		temp2.put("processstatus", shareEvent.getProcessstatus().toString());
		Integer eventid=shareEvent.getEventid();
		boolean isforwarded=this.eventService.checkClipEventForwardedORNOT(eventid,currentUser.getId());
		temp2.put("isforward", isforwarded);
		int result=this.eventService.getInviteEventInterestORNOT(eventid, currentUser.getId());
		if(result>0){
			ClipEvent interestedevent=this.eventService.getDetailClipEvent(result);
			temp2.put("clocktime", interestedevent.getAlerttime());
			temp2.put("eventisinterested", 1);
		}else{
			temp2.put("eventisinterested", 0);
		}
		boolean isconfirmevent=false;
		if(reliedclip>0)
			isconfirmevent=this.eventService.checkIsInvitedEvent(reliedclip);
		else
			isconfirmevent=this.eventService.checkIsInvitedEvent(shareEvent.getEventid());
		temp2.put("isconfirmevent", isconfirmevent);
		return temp2;
	}
	
	public Map orgRequestEvents(RequestEvent requestEvent,User currentUser,Calendar now){
		Map<String,Object> temp2=new LinkedHashMap<String,Object>();
		boolean isself=requestEvent.getUid()==currentUser.getId();
		int reliedclip=requestEvent.getReliedclip();
		int uid=requestEvent.getUid();
		
		temp2.put("isself", false);
		String contactname=this.contactService.getUserName(currentUser.getId(),uid);
		temp2.put("unameid",uid);
		temp2.put("uname", contactname);
		List urllist=this.userService.getUserLogoByUid(uid);
		temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(0)));
		temp2.put("unameslogo", ToolsUtils.xmlCDATA(urllist.get(1)));
		temp2.put("pname", "");
		temp2.put("cliptype", 4);
		temp2.put("shareid", requestEvent.getShareid());
		temp2.put("starttime", requestEvent.getBegintime().toString());
		temp2.put("endtime", requestEvent.getEndtime().toString());
		temp2.put("iswholeday", requestEvent.getIswholeday().toString());
		String content=requestEvent.getContent();
		content=content==null||content.length()==0?"":content;
		temp2.put("content", ToolsUtils.xmlCDATA(URLReplace.replace(content)));
		temp2.put("address", ToolsUtils.xmlCDATA(requestEvent.getAddress()));
		temp2.put("clipstatus", 1);
		temp2.put("title", ToolsUtils.xmlCDATA(requestEvent.getTitle()));
		temp2.put("eventtype", 1);
		temp2.put("seid", 0);
		ClipeventPic clipeventPic=this.eventService.getClipeventPic(requestEvent.getEventid());
		if(clipeventPic!=null){
			temp2.put("clipeventpic", clipeventPic.getPicurl());
		}else{
			temp2.put("clipeventpic", "");
		}
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		int timesituation=0;
		if(requestEvent.getBegintime()>=firstDate.getTimeInMillis()&&requestEvent.getBegintime()<secondDate.getTimeInMillis()){
			timesituation= -1;
		}else if(requestEvent.getEndtime()>=firstDate.getTimeInMillis()&&requestEvent.getEndtime()<secondDate.getTimeInMillis()&&requestEvent.getIswholeday()==0){
			timesituation= 1;
		}else{
			timesituation= 0;
		}
		
		temp2.put("timesituation", timesituation);
		temp2.put("reliedclip", requestEvent.getReliedclip().toString());
		temp2.put("processstatus", requestEvent.getProcessstatus().toString());
//		temp2.put("date", firstDateOfWeek.getTimeInMillis());
//		Integer eventid=requestEvent.getEventid();
//		ClipEvent acceptShareEvent=this.eventService.getUserAcceptShareEvent(eventid, currentUser.getId());
//		if(acceptShareEvent!=null){
//			temp2.put("isshareaccept", true);
//			temp2.put("acceptshareid",acceptShareEvent.getShareid()!=null&&acceptShareEvent.getShareid().length()>0?acceptShareEvent.getShareid():"");
//		}else
//			temp2.put("isshareaccept", false);
		return temp2;
	}
	
	public Map orgAllClipEvents(ClipEvent clipevent,User currentUser,Calendar now){
		Map<String,Object> temp2=new LinkedHashMap<String,Object>();
		boolean isself=clipevent.getUid()==currentUser.getId();
		int reliedclip=clipevent.getReliedclip();
		int cliptype=clipevent.getCliptype();
		// cliptype=1,3,4
		int uid=clipevent.getUid();
//		int rooteventid=0;
		boolean isccclip=false;
		if(reliedclip>0){
			//只可能在cliptype=3的情况下，好友的找朋友档期
//			rooteventid=reliedclip;
			ClipEvent parentEvent=this.eventService.getDetailClipEvent(reliedclip);
			if(parentEvent==null)
				return null;
			if(parentEvent.getUid()==currentUser.getId()){
				//不可能
				return null;
			}else{
				temp2.put("isself", false);
				String contactname=this.contactService.getUserName(currentUser.getId(),uid);
				if(parentEvent.getUid()==uid){
					//不可能
					return null;
				}else{
					temp2.put("unameid",parentEvent.getUid());
					temp2.put("uname", this.contactService.getUserName(currentUser.getId(),parentEvent.getUid()));
					List urllist=this.userService.getUserLogoByUid(parentEvent.getUid());
					temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(0)));
					temp2.put("unameslogo", ToolsUtils.xmlCDATA(urllist.get(1)));
					User parentUser=this.userService.getUserById(parentEvent.getUid());
					if(parentUser.getUsertype()==2){
						isccclip=true;
						temp2.put("clipiscc", 1);
					}else{
						temp2.put("clipiscc", 0);
					}
				}
			}
			temp2.put("eventisinterested", 1);
		}else{
//			rooteventid=clipevent.getId();
			if(uid==currentUser.getId()){
				temp2.put("isself", true);
				temp2.put("uname", "我");
				temp2.put("unameid", currentUser.getId());
				List urllist=this.userService.getUserLogoByUid(currentUser.getId());
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(0)));
				temp2.put("unameslogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				temp2.put("pname", "");
				temp2.put("eventisinterested", 0);
				temp2.put("clipiscc", 0);
			}else{
				//说明是订阅档期
				isccclip=true;
				temp2.put("isself", false);
				String contactname=this.contactService.getUserName(currentUser.getId(),uid);
				temp2.put("unameid",uid);
				temp2.put("uname", contactname);
				List urllist=this.userService.getUserLogoByUid(uid);
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(0)));
				temp2.put("unameslogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				temp2.put("pname", "");
				int result=this.eventService.getInviteEventInterestORNOT(clipevent.getId(), currentUser.getId());
				temp2.put("eventisinterested", result>0?1:0);
				temp2.put("clipiscc", 1);
			}
		}
		temp2.put("seid", clipevent.getSeid());
		if(clipevent.getSeid()>0){
			ClipEvent sourceEvent=this.eventService.getDetailClipEvent(clipevent.getSeid());
			User sourceUser=this.userService.getUserById(sourceEvent.getUid());
			temp2.put("sourceeventid", sourceEvent.getId());
			temp2.put("sourceuid", sourceEvent.getUid());
			temp2.put("sourceuname", this.contactService.getUserName(currentUser.getId(), sourceUser.getId()));
			List sourceuserlist=this.userService.getUserLogoByUid(sourceUser.getId());
			temp2.put("sourceulogo", sourceuserlist.get(1));
			
		}
		ClipeventPic clipeventPic=this.eventService.getClipeventPic(clipevent.getId());
		if(clipeventPic!=null){
			temp2.put("clipeventpic", clipeventPic.getPicurl());
		}else{
			temp2.put("clipeventpic", "");
		}
		temp2.put("cliptype", cliptype);
		temp2.put("shareid", clipevent.getShareid());
		temp2.put("starttime", clipevent.getBegintime().toString());
		temp2.put("endtime", clipevent.getEndtime().toString());
		temp2.put("invitetime", clipevent.getInvitetime().toString());
		temp2.put("iswholeday", clipevent.getIswholeday().toString());
		String content=clipevent.getContent();
		content=content==null||content.length()==0?"":content;
		temp2.put("content", ToolsUtils.xmlCDATA(URLReplace.replace(content)));
		temp2.put("clipstatus", clipevent.getStatus());
		temp2.put("title", ToolsUtils.xmlCDATA(clipevent.getTitle()));
		temp2.put("eventtype", clipevent.getEventtype());
		int timesituation=checkShareEventTimeSituation(clipevent);
		temp2.put("timesituation", timesituation);
		temp2.put("reliedclip", clipevent.getReliedclip().toString());
		eventService.updateOverDeadlinetime(clipevent,now);
		temp2.put("processstatus", clipevent.getProcessstatus().toString());
		temp2.put("clocktime", clipevent.getAlerttime().toString());
		
		//temp2.put("date", firstDateOfWeek.getTimeInMillis());
		if(cliptype==3&&isccclip==false){
			boolean isforward=this.eventService.checkClipEventForwardedORNOT(reliedclip>0?reliedclip:clipevent.getId(), currentUser.getId());
			temp2.put("isforward", isforward);
			boolean isconfirmevent=false;
			if(reliedclip>0)
				isconfirmevent=this.eventService.checkIsInvitedEvent(reliedclip);
			else
				isconfirmevent=this.eventService.checkIsInvitedEvent(clipevent.getId());
			temp2.put("isconfirmevent", isconfirmevent);
		}
		return temp2;
	}
	/**
	 * @param shareEvent
	 * @return 	0 	进行中
	 * 		 	1	今天结束
	 * 			-1	今天开始
	 */
	private int checkShareEventTimeSituation(ClipEvent clipEvent){
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		if(clipEvent.getBegintime()>=firstDate.getTimeInMillis()&&clipEvent.getBegintime()<secondDate.getTimeInMillis()){
			return -1;
		}else if(clipEvent.getEndtime()>=firstDate.getTimeInMillis()&&clipEvent.getEndtime()<secondDate.getTimeInMillis()&&clipEvent.getIswholeday()==0){
			return 1;
		}else{
			return 0;
		}
	}
	
	public void loadSubscribeEvents(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		int lastcurrentpage=-1;
		long lastquerycreattime=0l;
		if(needmore==1){
			lastquerycreattime=(Long) session.getAttribute("SUBSCRIBE_"+SessionAttributeKey.LASTQUERYCREATETIME);
			lastcurrentpage=(Integer) session.getAttribute("SUBSCRIBE_"+SessionAttributeKey.LASTQUERYPAGENUM);
		}
		List list=this.eventService.getSubscribeEvents(currentUser.getId(),ids,firstDate,lastquerycreattime,lastcurrentpage+1,CALENDARPAGESIZE);
		if(list.size()>0){
			ClipEvent clipevent=(ClipEvent) list.get(0);
			session.setAttribute("SUBSCRIBE_"+SessionAttributeKey.LASTQUERYCREATETIME,clipevent.getCreatetime());
			session.setAttribute("SUBSCRIBE_"+SessionAttributeKey.LASTQUERYPAGENUM,0);
		}
		Calendar now=Calendar.getInstance();
		Map map=new LinkedHashMap();
		for (Object object : list) {
			ClipEvent event=(ClipEvent) object;
			Map submap=orgAllClipEvents(event,currentUser,now);
			map.put("ClipEvent id="+event.getId(),submap);
		}
		Struts2Utils.renderXml(ToolsUtils.getDOM(map));
	}
	
	public void uploadClipPic(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		if(clippic!=null){
			long filelength=clippic.length();
			double re=(new BigDecimal(filelength).divide(new BigDecimal(1024)).divide(new BigDecimal(1024)).doubleValue());
			if(re>5.0d){
				Struts2Utils.renderHtml("<script>parent.errorUploadCallback()</script>");
				return;
			}
		}
		session.setAttribute(currentUser.getId()+"_"+clippictime, true);
		ClipeventPic clipeventPic=this.picService.saveTempClippic(currentUser.getId(), clippic, clippicFileName);
		boolean flag=(Boolean) session.getAttribute(currentUser.getId()+"_"+clippictime);
		if(flag)
			Struts2Utils.renderHtml("<script>parent.uploadCallback("+clipeventPic.getId()+",'"+ToolsUtils.getApachePhysicalURL().get("webClippicPath")+"/"+clipeventPic.getPicurl()+"')</script>");
		else{
			this.picService.deleteTempClippic(clipeventPic);
			Struts2Utils.renderHtml("");
		}
	}
	
	public void deleteUploadClippic(){
		this.picService.deleteUploadClippic(picid);
		Struts2Utils.renderHtml("{result:1}");
	}
	
	public void cancelUploadClippic(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		session.setAttribute(currentUser.getId()+"_"+clippictime, false);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result", 1);
		Struts2Utils.renderJson(jsonObject.toJSONString());
	}
	
	public void getShareCount(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		JSONObject jsonObject=new JSONObject();
		ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
		List clist=this.eventService.getStatAboutClipEvent(clipEvent,currentUser);
		Calendar now=Calendar.getInstance();
		if(currentUser.getUsertype()==2){
			jsonObject.put("commentcount", clist.get(0));
		}else{
			if(clipEvent.getEndtime()>now.getTimeInMillis()){
				int dealeventid=0;
				if(clipEvent.getSeid()>0){
					dealeventid=clipEvent.getSeid();
				}else{
					dealeventid=clipEvent.getId();
				}
				List flist=this.eventService.findHaveInterestFriends(currentUser.getId(),dealeventid);
				JSONArray array=new JSONArray();
				for (Object object : flist) {
					Integer uid=(Integer) object;
					JSONObject temp=new JSONObject();
					String smalllogo=(String) this.userService.getUserLogoByUid(uid).get(1);
					String name=this.contactService.getUserName(currentUser.getId(), uid);
					temp.put("uid",uid);
					temp.put("name",name);
					temp.put("smalllogo",smalllogo);
					array.add(temp);
				}
				jsonObject.put("users", array);
			}
			List<Long> checklist=this.eventService.checkUserStatusAboutShareEvent(clipEvent,currentUser);
			jsonObject.put("interestcount", clist.get(0));
			jsonObject.put("forwardcount", clist.get(1));
			jsonObject.put("favorcount", clist.get(2));
			jsonObject.put("commentcount", clist.get(3));
			jsonObject.put("isinterest",checklist.get(0)>0l?true:false);
			jsonObject.put("isfavor",checklist.get(1)>0l?true:false);
		}
		Struts2Utils.renderJson(jsonObject.toJSONString());
	}
	
	public void getInviteCount(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		JSONObject jsonObject=new JSONObject();
		ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
		List clist=this.eventService.getStatAboutClipEvent(clipEvent,currentUser);
		jsonObject.put("commentcount", clist.get(0));
		List share2users=null;
		int dealeventid=0;
		if(clipEvent.getReliedclip()>0)
			dealeventid=clipEvent.getReliedclip();
		else
			dealeventid=clipEvent.getId();
		share2users=this.eventService.getShare2users(dealeventid);
		List interestusers=this.eventService.findHaveInterestFriends(currentUser.getId(),dealeventid);
		JSONArray array=new JSONArray();
		List temp1=new ArrayList();//放已感兴趣的
		List temp2=new ArrayList();//放未感兴趣的
		for (Object object : share2users) {
			Sharemapping sharemapping=(Sharemapping) object;
			int uid=sharemapping.getShare2uid();
			if(uid==currentUser.getId())
				continue;
			if(interestusers.contains(uid)){
				temp1.add(uid);
			}else{
				temp2.add(uid);
			}
		}
		for (Object object : temp1) {
			Integer uid=(Integer) object;
			JSONObject temp=new JSONObject();
			List logos=this.userService.getUserLogoByUid(uid);
			String biglogo=(String) logos.get(0);
			String smalllogo=(String) logos.get(1);
			String name=this.contactService.getUserName(currentUser.getId(), uid);
			temp.put("uid",uid);
			temp.put("name",name);
			temp.put("smalllogo",smalllogo);
			temp.put("biglogo",biglogo);
			temp.put("eventisinterested", 1);
			array.add(temp);
		}
		for (Object object : temp2) {
			Integer uid=(Integer) object;
			JSONObject temp=new JSONObject();
			List logos=this.userService.getUserLogoByUid(uid);
			String biglogo=(String) logos.get(0);
			String smalllogo=(String) logos.get(1);
			String name=this.contactService.getUserName(currentUser.getId(), uid);
			temp.put("uid",uid);
			temp.put("name",name);
			temp.put("smalllogo",smalllogo);
			temp.put("biglogo",biglogo);
			temp.put("eventisinterested", 0);
			array.add(temp);
		}
		jsonObject.put("users", array);
		Struts2Utils.renderJson(jsonObject.toJSONString());
	}
	
	public void getRequestCount(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		JSONObject jsonObject=new JSONObject();
		ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
		List clist=this.eventService.getStatAboutClipEvent(clipEvent,currentUser);
		jsonObject.put("commentcount", clist.get(0));
		Struts2Utils.renderJson(jsonObject.toJSONString());
	}
	
	public void confirmEvent(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		JSONObject jsonObject=new JSONObject();
		boolean result=this.eventService.confirmEvent(eventid, currentUser, null);
		if(result){
			jsonObject.put("result", 1);
		}else{
			jsonObject.put("result", 0);
			jsonObject.put("errormsg", "无权操作或档期不存在");
		}
		Struts2Utils.renderJson(jsonObject.toJSONString());
	}
	
	public void shieldClipmessage(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		JSONObject jsonObject=new JSONObject();
		boolean result=this.eventService.shieldClipmessage(eventid, currentUser,true);
		if(result){
			jsonObject.put("result", 1);
		}else{
			jsonObject.put("result", 0);
			jsonObject.put("errormsg", "无权操作或档期不存在");
		}
		Struts2Utils.renderJson(jsonObject.toJSONString());
	}
	
	public void acceptClipmessage(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		JSONObject jsonObject=new JSONObject();
		boolean result=this.eventService.shieldClipmessage(eventid, currentUser,false);
		if(result){
			jsonObject.put("result", 1);
		}else{
			jsonObject.put("result", 0);
			jsonObject.put("errormsg", "无权操作或档期不存在");
		}
		Struts2Utils.renderJson(jsonObject.toJSONString());
	}
	
	private Integer eventid;
	private String title;
	private String address="";
	private String content="";
	private String eventtype;
	private Long begintime=0l;
	private Long endtime=0l;
	private Long alerttime=0l;
	private Integer mobilenotice=0;
	private Long deadlinetime=0l;
	private String mobilelist="";
	private Integer cliptype=1;
	private int status;
	private Integer year;
	private int month;
	private int date;
	private Integer isImport;
	private String eventids;
	private Integer iswholeday;
	private String share2userlist;
	private int specialtype;
	private String queryby;
	private int needmore;
	private int move;
	private String ids;
	private File clippic;
	private String clippicFileName;
	private long clippictime;
	private int picid;
	public Integer getIsImport() {
		return isImport;
	}

	public void setIsImport(Integer isImport) {
		this.isImport = isImport;
	}

	public Integer getCliptype() {
		return cliptype;
	}
	public void setCliptype(Integer cliptype) {
		this.cliptype = cliptype;
	}
	public Integer getEventid() {
		return eventid;
	}
	public void setEventid(Integer eventid) {
		this.eventid = eventid;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getEventtype() {
		return eventtype;
	}
	public void setEventtype(String eventtype) {
		this.eventtype = eventtype;
	}
	public Long getBegintime() {
		return begintime;
	}
	public void setBegintime(Long begintime) {
		this.begintime = begintime;
	}
	public Long getEndtime() {
		return endtime;
	}
	public void setEndtime(Long endtime) {
		this.endtime = endtime;
	}
	public Long getAlerttime() {
		return alerttime;
	}
	public void setAlerttime(Long alerttime) {
		this.alerttime = alerttime;
	}
	public Long getDeadlinetime() {
		return deadlinetime;
	}

	public void setDeadlinetime(Long deadlinetime) {
		this.deadlinetime = deadlinetime;
	}

	public Integer getMobilenotice() {
		return mobilenotice;
	}

	public void setMobilenotice(Integer mobilenotice) {
		this.mobilenotice = mobilenotice;
	}

	public String getMobilelist() {
		return mobilelist;
	}

	public void setMobilelist(String mobilelist) {
		this.mobilelist = mobilelist;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public String getEventids() {
		return eventids;
	}

	public void setEventids(String eventids) {
		this.eventids = eventids;
	}

	public Integer getIswholeday() {
		return iswholeday;
	}

	public void setIswholeday(Integer iswholeday) {
		this.iswholeday = iswholeday;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDate() {
		return date;
	}

	public void setDate(int date) {
		this.date = date;
	}

	public int getSpecialtype() {
		return specialtype;
	}

	public void setSpecialtype(int specialtype) {
		this.specialtype = specialtype;
	}

	public String getShare2userlist() {
		return share2userlist;
	}

	public void setShare2userlist(String share2userlist) {
		this.share2userlist = share2userlist;
	}

	public String getQueryby() {
		return queryby;
	}

	public void setQueryby(String queryby) {
		this.queryby = queryby;
	}

	public int getNeedmore() {
		return needmore;
	}

	public void setNeedmore(int needmore) {
		this.needmore = needmore;
	}

	public int getMove() {
		return move;
	}

	public void setMove(int move) {
		this.move = move;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public File getClippic() {
		return clippic;
	}

	public void setClippic(File clippic) {
		this.clippic = clippic;
	}

	public String getClippicFileName() {
		return clippicFileName;
	}

	public void setClippicFileName(String clippicFileName) {
		this.clippicFileName = clippicFileName;
	}

	public int getPicid() {
		return picid;
	}

	public void setPicid(int picid) {
		this.picid = picid;
	}

	public long getClippictime() {
		return clippictime;
	}

	public void setClippictime(long clippictime) {
		this.clippictime = clippictime;
	}
	
	
}

